package com.example.yuejianapp14.Activity.Fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.Activity.SearchActivity;
import com.example.yuejianapp14.Activity.Adapter.ActAdapter;
import com.example.yuejianapp14.Activity.Adapter.CoachAdapter;
import com.example.yuejianapp14.Activity.Adapter.GameAdapter;
import com.example.yuejianapp14.Activity.Adapter.Lunboadapter;
import com.example.yuejianapp14.Activity.JavaBean.Act;
import com.example.yuejianapp14.Activity.JavaBean.Coach;
import com.example.yuejianapp14.Activity.JavaBean.Game;
import com.example.yuejianapp14.Activity.Listener.ActListener;
import com.example.yuejianapp14.Activity.Listener.CoachListener;
import com.example.yuejianapp14.Activity.Listener.GameListener;
import com.example.yuejianapp14.Activity.Model.ActivityModel;
import com.example.yuejianapp14.Activity.Model.CoachModel;
import com.example.yuejianapp14.Activity.Model.GameModel;
import com.example.yuejianapp14.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {
    private RecyclerView recyclerView;
    private List mDataList;
    private View view;
    private ViewPager vpager_one;
    private ArrayList<Integer> aList;
    private Lunboadapter mAdapter;
    private int page = 0;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(page >= aList.size()){
                page = 0;
            }
            vpager_one.setCurrentItem(page);
            page++;
            handler.sendEmptyMessageDelayed(page,4000);
        }
    };


    public MainFragment() {
        // Required empty public constructor
    }
    ActListener actListener = new ActListener() {
        @Override
        public void onResponse(List<Act> beanList) {
            mDataList=beanList;
            ActAdapter adapter = new ActAdapter(getActivity(),mDataList);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    CoachListener coachListener = new CoachListener() {
        @Override
        public void onResponse(List<Coach> beanList) {
            mDataList=beanList;
            CoachAdapter adapter = new CoachAdapter(getActivity(),mDataList);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };


    GameListener gameListener = new GameListener() {
        @Override
        public void onResponse(List<Game> beanList) {
            mDataList=beanList;
            GameAdapter adapter = new GameAdapter(getActivity(),mDataList);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };








    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view= inflater.inflate(R.layout.fragment_main, container, false);
        getActData();
        initActRecyclerView();
        vpager_one =(ViewPager) view.findViewById(R.id.pimg);
        aList = new ArrayList<Integer>();
        aList.add(R.drawable.boy);
        aList.add(R.drawable.man);
        aList.add(R.drawable.kobe);
        Lunboadapter adapter = new Lunboadapter(getContext(),aList);
        vpager_one.setAdapter(adapter);
        handler.sendEmptyMessage(0);
        return view;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
              super.onActivityCreated(savedInstanceState);
              final Button act = (Button) getActivity().findViewById(R.id.id_act);
              final Button coach = (Button) getActivity().findViewById(R.id.id_coach);
              final Button game = (Button) getActivity().findViewById(R.id.id_game);
              final ImageButton find=(ImageButton) getActivity().findViewById(R.id.find);

              find.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent intent = new Intent(getActivity(), SearchActivity.class);
                      startActivity(intent);
                  }
              });

              act.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      getActData();
                      initActRecyclerView();
//                     Toast.makeText(getActivity(), "精选活动", Toast.LENGTH_LONG).show();
                      act.setTextColor(Color.parseColor("#66ff33"));
                      coach.setTextColor(Color.parseColor("#000000"));
                      game.setTextColor(Color.parseColor("#000000"));
                  }
              });

        coach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getCoachData();
                initCoachRecyclerView();
                Toast.makeText(getActivity(), "权威大师", Toast.LENGTH_LONG).show();
                act.setTextColor(Color.parseColor("#000000"));
                coach.setTextColor(Color.parseColor("#66ff33"));
                game.setTextColor(Color.parseColor("#000000"));

            }
        });


        game.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getGameData();
                initCoachRecyclerView();
                Toast.makeText(getActivity(), "精选比赛", Toast.LENGTH_LONG).show();
                act.setTextColor(Color.parseColor("#000000"));
                coach.setTextColor(Color.parseColor("#000000"));
                game.setTextColor(Color.parseColor("#66ff33"));

            }
        });





      }




    private void getActData() {
        ActivityModel activityModel =new ActivityModel();
        activityModel.getActList(actListener);
    }

    private void getCoachData() {
        CoachModel coachModel =new CoachModel();
        coachModel.getCoachList(coachListener);
    }


    private void getGameData() {
        GameModel gameModel =new GameModel();
        gameModel.getGameList(gameListener);
    }



    private void initActRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_main);
        recyclerView.setLayoutManager(new LinearLayoutManager((this.getActivity())));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    private void initCoachRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_main);
        recyclerView.setLayoutManager(new LinearLayoutManager((this.getActivity())));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }



}


